<%@ include file = "/common/ini.jsp"%> 
<%@ include file = "/common/_log4j.jsp"%> 
<%@ include file = "/common/_ApplicationContext_init.jsp"%>
<%
        logger.info(request.getMethod()  + "============================================================" );
        logger.info("mvc.c comming ... :) ");
        String[] mms = request.getParameter("serviceURL").split("\\.");
        String moduleId = mms[0]; // 客户端必须提交过来
        String modelId = mms[1]; // 客户端必须提交过来
        String serviceId = mms[2]; // 客户端必须提交过来
        logger.debug("  >>  module : " + moduleId);
        logger.debug("  >>  model : " + modelId);
        logger.debug("  >>  service : " + serviceId);    
        ModuleConfigReader mcReader = new ModuleConfigReader(moduleId);
        FormBeanMaker fbMaker = new FormBeanMaker(request,
                                                  mcReader.getElementByModelIdNServiceId(modelId,serviceId),
                                                  new FormBeanConfigReader(moduleId,modelId),
                                                  applicationContext);
        // 校验构造出来formBean放入request
        Object[] beanIdAndObj = fbMaker.making();        
        if(beanIdAndObj  != null)
        {
            request.setAttribute((String)beanIdAndObj[0],beanIdAndObj[1]);
        }
        request.setAttribute("ValidatedResult",fbMaker.getVR());
        // 开始校验
        if(fbMaker.getVR().isAllValidated())        
        {
                logger.info("通过校验拉 ^o^ ..");
                ServiceTO serviceTO = mcReader.getServiceTO(modelId,serviceId);
                request.setAttribute("serviceTO",serviceTO);
                AbstractService aService = (AbstractService)applicationContext.getBean(serviceId,AbstractService.class);
                // 初始化操作日志
                %><%@ include file = "/common/operatinglog/_before_Service_exceute.jsp"%><%
                try
                {
                        
                        // 执行服务
                        aService.executeService(request);
                        // 记录日志
                        String result = "成功";
                        %><%@ include file = "/common/operatinglog/_after_Service_exceute.jsp"%><%
                        // 跳转
                        ViewTO suViewTO = mcReader.getViewTO(modelId,serviceId,"successful");
                        String succURL = suViewTO.getUrl().replaceAll("\\$\\{ContextPath\\}",request.getContextPath());
                        if(request.getParameter("forwardPageUrl")!=null)
                        {
                            succURL = request.getParameter("forwardPageUrl");
                            logger.info("跳转到特殊的定制页面" + succURL);
                        }
                        logger.info("重定向到" + succURL);
                        if(suViewTO.isForward())
                        {
                                logger.debug("forward()");
                                ServletContext sc = getServletContext();
                                // forward 不支持上下文路径的绝对路径，因此将上下文路径替换掉
                                RequestDispatcher rd = sc.getRequestDispatcher(succURL.replaceAll(request.getContextPath(),""));     //定向的页面
                                rd.forward(request, response);
                        }
                        else
                        {
                                logger.debug("redirect()");
                                response.sendRedirect(succURL);
                        }
                }
                catch(Exception e)
                {
                        // 操作日志
                        String result = "失败";
                        %><%@ include file = "/common/operatinglog/_after_Service_exceute.jsp"%><%
                        // 跳转
                        logger.error("执行服务异常: " + e.toString() );
                        e.printStackTrace();
                        ViewTO faViewTO = mcReader.getViewTO(modelId,serviceId,"failed");
                        String faURL = faViewTO.getUrl().replaceAll("\\$\\{ContextPath\\}",request.getContextPath());
                        logger.info("重定向到" + faURL);                        
                        response.sendRedirect(faURL + "?exception=" + e.toString());                        
                }
        }
        else
        {
                logger.info("校验失败 凸-_-凸 ..");        
                // 回去吧
                ViewTO ivViewTO = mcReader.getViewTO(modelId,serviceId,"inValidated");
                // forward 不支持上下文路径的绝对路径，因此将上下文路径替换掉
                String ivURL = ivViewTO.getUrl().replaceAll("\\$\\{ContextPath\\}","");
                logger.info("重定(forward)向到" + ivURL);
                ServletContext sc = getServletContext();
                RequestDispatcher rd = sc.getRequestDispatcher(ivURL);     //定向的页面
                rd.forward(request, response);
        }
        logger.info("校验结果VR :" + request.getAttribute("ValidatedResult"));
        logger.info("-----------------------------------------------------------");
%>
